<?php
error_reporting(E_ALL);
require_once('xfb/xfb.php');
require('xfb/xfbpay.php');
class demo{
     public function __construct() {
		 //$date = 'gzy'.date('YmdHis',time());
		 $this->shang_hu_hao = '000201505130444481';//商户号
		 $this->mishi='9aa5194b70aadf1da90745c39d5b89fd';//密匙
		 $this->pay_sn='2016123004156328945';//订单号
		 $this->pay_amount_online = '0.01';//金额
		 $this->xfbpay = new Xfbpay();
     }
	 //网关下单  直连模式
	 public function gateway_order()
	 {
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['totalfee']=$this->pay_amount_online;//消费总金额
		$xfbarr['typemethod']='xfpay';//消费总金额
		$xfbarr['goodsname']='商品名称';//
		$xfbarr['body']='祥付宝支付';//订单标题
		$xfbarr['paytype']=1;//支付方式
		$xfbarr['bankcode']='ICBC';//银行简码   工商银行
		$xfbarr['banktype']='C,DC';//银行简码
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,3);		
		file_put_contents('./zlms/'.$this->pay_sn.'.txt',$xfb_erweima);
		echo '内容写入文件夹xlms里面'.$this->pay_sn.'.txt';
	 }
	 //支付查询/退款单查询
	 public function select_order()
	 {
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['typemethod']='queryOrderid';//商户号
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,3);
		$dom = new DOMDocument();
		$dom->loadXML($xfb_erweima);
		$xfb = new Xfb();
		$result = $xfb->getArray($dom->documentElement);
		$result = $this->xfbpay->dispose_array($result);
		var_dump($result);

	 }
	 //撤销订单
	 public function cancel()
	 {		
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['typemethod']='revoke';
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,3);
		$dom = new DOMDocument();
		$dom->loadXML($xfb_erweima);
		$xfb = new Xfb();
		$result = $xfb->getArray($dom->documentElement);
		$result = $this->xfbpay->dispose_array($result);
		var_dump($result);
	 }
	 //退款
	 public function refund()
	 {		
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['typemethod']='refund';
		$xfbarr['refundamount']=0.01;
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,3);
		$dom = new DOMDocument();
		$dom->loadXML($xfb_erweima);
		$xfb = new Xfb();
		$result = $xfb->getArray($dom->documentElement);
		$result = $this->xfbpay->dispose_array($result);
		var_dump($result);
	 }
	 //多人转账
	 public function multiple_transfers()
	 {
        $xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['totalamount']=0.01;
		$xfbarr['count']=2;
		$xfbarr['typemethod']='transfer';
		$arr_list=array(
			array(
				'cardno'=>'622620010000000111',
				'name'=>'皇子要1',
				'zbank'=>'民生银行1',
				'corname'=>'民生银行上地支行1',
				'fbank'=>'民生银行上地支行1',
				'type'=>'私',
				'province'=>'北京市',
				'city'=>'昌平',
				'amount'=>'0.01',
				'phone'=>'13699252914',
				'cert'=>'身份证',
				'certno'=>'32032119880505361X',
				'note'=>'测试请通过'
			),
			array(
				'cardno'=>'622620010000000000',
				'name'=>'皇子要',
				'zbank'=>'民生银行1',
				'corname'=>'民生银行上地支行1',
				'fbank'=>'民生银行上地支行1',
				'type'=>'私',
				'province'=>'北京市',
				'city'=>'昌平',
				'amount'=>'0.01',
				'phone'=>'13699252914',
				'cert'=>'身份证',
				'certno'=>'430527199012304238',
				'note'=>'测试请通过'
			)
		);
		$xfbarr['replyList']=$arr_list;
		$xfb_erweima = $this->xfbpay->multiple_transfers($xfbarr,$this->mishi,3);
		$dom = new DOMDocument();
		$dom->loadXML($xfb_erweima);
		$xfb = new Xfb();
		$result = $xfb->getArray($dom->documentElement);
		$result = $this->xfbpay->dispose_array($result);
		var_dump($result);
	 }
	 //转账查询
	 public function transfer_query()
	 {
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['typemethod']='queryTransfer';
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,3);
		$dom = new DOMDocument();
		$dom->loadXML($xfb_erweima);
		$xfb = new Xfb();
		$result = $xfb->getArray($dom->documentElement);
		$result = $this->xfbpay->dispose_array($result);
		var_dump($result);
	 }
	 //异步通知
	 public function asynchronous_notific()
	 {
		$get_sign = $_GET['sign'];
		//$sign='F77EB4866C82416E78478DC40E2F74EF';
		$arr['orderid'] = $_GET['orderid'];//商户订单号
		$arr['outtranno'] = $_GET['outtranno'];//祥付宝订单号
		$arr['tradestate'] = $_GET['tradestate'];//交易状态(TRADE_SUCCESS交易成功,TRADE_REFUND,退款，REVOKED 撤销)
		$arr['orderamt'] = $_GET['orderamt'];//交易金额
		//$arr['storecode'] = $_GET['storecode'];//店面号
		$arr['method'] = $_GET['method'];//支付方式，说明见1.14
		$arr['tradetime'] = $_GET['tradetime'];//交易时间 格式yyyyMMddhhmmss
		$arr['thirdorderid'] = $_GET['thirdorderid'];//微信/支付宝等第三方交易号	
		//$arr['oldorder'] = $_GET['oldorder'];原单号（退款会返回）
		foreach($arr as $key=>$value)
		{
		    $arrStr[]=$key.'='.$value;
		}
		$xfb = new Xfb();
		$sign = $xfb->sign($arrStr,$this->mishi);//生成sign
		if($get_sign==$sign)
		{
			if($arr['tradestate']=='TRADE_SUCCESS')
			{
				echo 'success';//成功
			}
		}
	 }
}
//$demo = new demo();
$demo->gateway_order();//网关下单 直连模式
//$demo->select_order();//支付查询/退款单查询
//$demo->asynchronous_notific();

?>